﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using stackFluent.Web;
using stackFluent.Web.Models;
using stackFluent.Application;
using stackFluent.Domain;
using Microsoft.Practices.Unity;

namespace stackFluent.Web.Controllers {
    public class AccountController : Controller {

        [Dependency]
        public IUserService UserService { get; set; }

        public ActionResult LogOn() {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl) {
            if (!ModelState.IsValid) {
                ModelState.AddModelError("", "输入验证不正确。");
                return View(model);
            }
            User user = UserService.UserValidate(model.Email, model.Password);
            if (user == null) {
                ModelState.AddModelError("", "提供的用户名或密码不正确。");
                return View(model);
            }

            FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) {
                return Redirect(returnUrl);
            } else {
                return RedirectToAction("Index", "Home");
            }
        }

        public ActionResult LogOff() {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Register() {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model) {
            if (!ModelState.IsValid) {
                ModelState.AddModelError("", "输入验证不正确。");
                return View(model);
            }
            if (UserService.UserCheckEmailExist(model.Email)) {
                ModelState.AddModelError("", "Email 已被其他用户使用，请换一个。");
                return View(model);
            }

            User user = new User {
                Email = model.Email,
                Name = model.Name,
                Password = model.Password
            };
            UserService.UserCreat(user);

            FormsAuthentication.SetAuthCookie(model.Name, false /* createPersistentCookie */);
            return RedirectToAction("Index", "Home");
        }

        [Authorize]
        public ActionResult ChangePassword() {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model) {
            if (!ModelState.IsValid) {
                ModelState.AddModelError("", "输入验证不正确。");
                return View(model);
            }
            if (UserService.UserChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword)) {
                return RedirectToAction("ChangePasswordSuccess");
            } else {
                ModelState.AddModelError("", "当前用户名或密码不正确。");
                return View(model);
            }
        }

        public ActionResult ChangePasswordSuccess() {
            return View();
        }
    }
}
